package com.smartHome.service.impl;

import com.smartHome.mapper.DeviceMapper;
import com.smartHome.mapper.RoomMapper;
import com.smartHome.pojo.entity.Room;
import com.smartHome.pojo.vo.RoomVO;
import com.smartHome.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void add(Room room) {
        roomMapper.add(room);
    }

    @Override
    public List<RoomVO> getByHouseId(Integer id) {
        List<Room> rooms = roomMapper.getByHouseId(id);
        return rooms.stream().map(room -> {
            RoomVO roomVO = new RoomVO();
            roomVO.setRoomId((room.getRoomId()));
            roomVO.setRoomName(room.getRoomName());
            roomVO.setBackgroundImage(room.getBackgroundImage());
            roomVO.setRoomType(room.getRoomType());
            roomVO.setTotalDevices(deviceMapper.countByRoomId(room.getRoomId()));
            roomVO.setOnlineDevices(deviceMapper.countOnlineByRoomId(room.getRoomId()));
            return roomVO;
        }).collect(Collectors.toList());
    }
    /**
     * 删除房间
     * @param roomId
     */
    @Override
    @Transactional
    public void deleteRoomById(Integer roomId) {
        //删除房间里的设备
        deviceMapper.deleteDeviceByRoomId(roomId);
        roomMapper.deleteRoomById(roomId);
    }

    /**
     * 获取屋子ID
     * @param roomId
     * @return
     */
    @Override
    public Integer getHouseId(Integer roomId) {
        return roomMapper.getHouseId(roomId);
    }
}
